package entity

import (
	"time"
	"xr-game-server/core/constants/db"
	"xr-game-server/core/migrate"
	"xr-game-server/core/snowflake"
	"xr-game-server/core/syndb"
)

const (
	// TbRoleMail 表名
	TbRoleMail db.TbName = "role_mails"
)

// 列名
const (
	RoleMailIsRead      db.TbCol = "is_read"
	RoleMailIsReceived  db.TbCol = "is_received"
	RoleMailReceiveTime db.TbCol = "receive_time"
	RoleMailRoleId      db.TbCol = "role_id"
	RoleMailType        db.TbCol = "type"
)

// RoleMail --------------------------------------------------邮件-----------------------------------------------------------------------------------
type RoleMail struct {
	migrate.MoreModel
	IsRead      bool       `gorm:"default:false;comment:是否已读"`
	IsReceived  bool       `gorm:"default:false;comment:是否已领取附件"`
	ReceiveTime *time.Time `gorm:"comment:领取时间"`
	RoleId      uint64     `gorm:"comment:角色id"`
	Type        int32      `gorm:"comment:邮件类型"`
}

func NewRoleMail(roleId uint64) *RoleMail {
	ret := &RoleMail{}
	ret.ID = snowflake.GetId()
	ret.SetRoleId(roleId)
	ret.SetUpdateAt(time.Now())
	ret.SetCreateAt(time.Now())
	return ret
}

func NewRoleMailForSysMail(roleId uint64, mailId uint64, typ int32) *RoleMail {
	ret := &RoleMail{}
	ret.ID = mailId
	ret.SetRoleId(roleId)
	ret.SetUpdateAt(time.Now())
	ret.SetCreateAt(time.Now())
	ret.SetDeletedAt(time.Now())
	ret.SetIsDeleted(false)
	ret.SetType(typ)
	return ret
}

func (receiver *RoleMail) SetType(typ int32) {
	receiver.Type = typ
	receiver.SetUpdateAt(time.Now())
	syndb.AddDataToLazyChan(TbRoleMail, RoleMailType, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: typ,
	})
}
func (receiver *RoleMail) SetRoleId(roleId uint64) {
	receiver.RoleId = roleId
	receiver.SetUpdateAt(time.Now())
	syndb.AddDataToLazyChan(TbRoleMail, RoleMailRoleId, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: roleId,
	})
}
func (receiver *RoleMail) SetIsRead(isRead bool) {
	receiver.IsRead = isRead
	receiver.SetUpdateAt(time.Now())
	syndb.AddDataToLazyChan(TbRoleMail, RoleMailIsRead, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: isRead,
	})
}

func (receiver *RoleMail) SetIsReceived(isReceived bool) {
	receiver.IsReceived = isReceived
	receiver.SetUpdateAt(time.Now())
	syndb.AddDataToLazyChan(TbRoleMail, RoleMailIsReceived, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: isReceived,
	})
}

func (receiver *RoleMail) SetReceiveTime(receiveTime *time.Time) {
	receiver.ReceiveTime = receiveTime
	receiver.SetUpdateAt(time.Now())
	syndb.AddDataToLazyChan(TbRoleMail, RoleMailReceiveTime, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: receiveTime,
	})
}

func (receiver *RoleMail) SetCreateAt(val time.Time) {
	receiver.CreatedAt = val
	syndb.AddDataToLazyChan(TbRoleMail, db.CreatedAtName, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}
func (receiver *RoleMail) SetUpdateAt(val time.Time) {
	receiver.UpdatedAt = val
	syndb.AddDataToLazyChan(TbRoleMail, db.UpdatedAtName, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}
func (receiver *RoleMail) SetDeletedAt(val time.Time) {
	receiver.DeletedAt = val
	syndb.AddDataToLazyChan(TbRoleMail, db.DeletedAtName, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}
func (receiver *RoleMail) SetIsDeleted(val bool) {
	receiver.IsDeleted = val
	syndb.AddDataToLazyChan(TbRoleMail, db.IsDeletedName, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}

func initMail() {

	migrate.AutoMigrate(&RoleMail{})
	syndb.RegLazyWithMiddle(TbRoleMail, RoleMailIsRead)
	syndb.RegLazyWithMiddle(TbRoleMail, RoleMailIsReceived)
	syndb.RegLazyWithMiddle(TbRoleMail, RoleMailReceiveTime)
	syndb.RegLazyWithMiddle(TbRoleMail, RoleMailType)
	syndb.RegLazyWithMiddle(TbRoleMail, RoleMailRoleId)

	syndb.RegLazyWithMiddle(TbRoleMail, db.CreatedAtName)
	syndb.RegLazyWithMiddle(TbRoleMail, db.UpdatedAtName)
	syndb.RegLazyWithMiddle(TbRoleMail, db.DeletedAtName)
	syndb.RegLazyWithMiddle(TbRoleMail, db.IsDeletedName)

}
